package CutLoad.components.magazine
{
	import CutLoad.Base;
	import CutLoad.cookies.CLoadImg;
	import CutLoad.cookies.Fun;
	import CutLoad.cookies.Painter;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class CMagazine_test extends Base
	{
		private var arrImgPath:Array;
		//下一页对象
		private var currentPage:Base;
		//实际显示的对象
		private var currentTruePage:Base;
		private var nextPage:Base;
		private var _page:uint;
		private var currentBitmapLeft:BitmapData;
		private var currentBitmapRight:BitmapData;
		//背面
		private var currentBitmapLeft_out:BitmapData;
		private var currentBitmapRight_out:BitmapData;
		private var touchPoint1:Sprite,touchPoint2:Sprite,touchPoint3:Sprite,touchPoint4:Sprite;
		private var touchPoint_type:Number=0;
		//
		private var currentPagePath:String="";
		private var nextPagePath:String="";
		private var prePagePath:String="";
		//层
		//触摸
		private var layout_0:Sprite;
		//下一页
		private var layout_1:Sprite;
		//原始效果
		private var layout_true:Sprite;
		//翻页效果
		private var layout_2_left:Sprite,layout_2_right:Sprite;
		//左右空页
		private var leftEmpty:BitmapData;
		private var rightEmpty:BitmapData;
		//
		private var pleft_pt:Point;
		private var pright_pt:Point;
		private var mousePoint_left:Point,mousePoint_right:Point;
		private var isMouseLeftRight:Boolean=true;
		//一个页面的左右宽度
		private var w2:Number,w2_1:Number;
		private var pageW:Number,pageH:Number;
		//
		private var isMouseUp:Boolean=true;
		//
		public function CMagazine_test(arrImgPath:Array,w:Number,h:Number,touchW:Number=20,touchH:Number=20)
		{
			_page=0;
			this.arrImgPath=arrImgPath;
			//层
			layout_1=new Sprite();
			layout_true=new Sprite();
			layout_2_left=new Sprite();
			layout_2_right=new Sprite();
			layout_0=new Sprite();
			//
			w2=w/2;
			w2_1=w-w2;
			this.pageW=w;
			this.pageH=h;
			//
			this.addChild(layout_1);
			this.addChild(layout_true);
			this.addChild(layout_2_left);
			layout_2_right.x=w-w/2;
			this.addChild(layout_2_right);
			this.addChild(layout_0);
			//生成4个接触点
			touchPoint1=Painter.getRect(touchW,touchH,0xff0000,0.5);
			touchPoint2=Painter.getRect(touchW,touchH,0xff0000,0.5);
			touchPoint3=Painter.getRect(touchW,touchH,0xff0000,0.5);
			touchPoint4=Painter.getRect(touchW,touchH,0xff0000,0.5);
			Fun.DisplayObjectSetXY(touchPoint1,0,0,layout_0);
			Fun.DisplayObjectSetXY(touchPoint2,w-touchW,0,layout_0);
			Fun.DisplayObjectSetXY(touchPoint3,w-touchW,h-touchH,layout_0);
			Fun.DisplayObjectSetXY(touchPoint4,0,h-touchH,layout_0);
			touchPoint1.buttonMode=true;
			touchPoint2.buttonMode=true;
			touchPoint3.buttonMode=true;
			touchPoint4.buttonMode=true;
			//
			pleft_pt=new Point(0,0);
			pright_pt=new Point(0,0);
			mousePoint_left=new Point(0,0);
			mousePoint_right=new Point(0,0);
			//
			touchPoint1.addEventListener(MouseEvent.MOUSE_DOWN,down_touchPoint);
			touchPoint2.addEventListener(MouseEvent.MOUSE_DOWN,down_touchPoint);
			touchPoint3.addEventListener(MouseEvent.MOUSE_DOWN,down_touchPoint);
			touchPoint4.addEventListener(MouseEvent.MOUSE_DOWN,down_touchPoint);
			//
			leftEmpty=new BitmapData(w/2,h);
			rightEmpty=new BitmapData(w-w/2,h);
			//
			currentBitmapLeft_out=this.leftEmpty;
			page=0;
			showHidePageFlip(false);
			//
			this.addEventListener(Event.ENTER_FRAME,enter_frame);
		}
		public function get page():uint{
			return _page;
		}
		//直接显示下一页
		private function _NextPage():void{
			if(_page+1>pageCount-1) return;
			page=_page+1;
		}
		//直接显示上一页
		private function _PrePage():void{
			if(_page-1<0) return;
			page=_page-1;
		}
		public function get pageCount():Number{
			return this.arrImgPath.length;
		}
		public function set page(num:uint):void{
			if(num<0||num>arrImgPath.length-1) return;
			var path:String=arrImgPath[num];
			currentPagePath=path;
			path=path.toLocaleLowerCase();
			if(path.indexOf(".swf")!=-1){
				
			}else if(path.indexOf(".jpg")!=-1||path.indexOf(".png")!=-1||path.indexOf(".gif")!=-1){
				setCurrentPage(new CLoadImg(path,0,0,loadCompetImg));
			}
		}
		private function loadCompetImg(me:CLoadImg):void{
			var urlTem:String=me.imgPath.toLocaleLowerCase();
			if(currentPagePath==urlTem||nextPagePath==urlTem||prePagePath==nextPagePath){
				//加载完成，当果显示的当前图片，要刷新一下
				updateCurrentPageBitmapData(me);
			}
		}
		//当前页显示出来，只是为了获得bitmapdata
		private function setCurrentPage(base:Base):void{
			if(currentTruePage!=null){
				currentTruePage.killMe();
				currentTruePage=null;
			}
			currentTruePage=base;
			currentTruePage.show(layout_true);
		}
		//刷新当前页,如果当前页还没有加载到图片，就到这一页时，需要再调用一次
		private function updateCurrentPageBitmapData(target:Base):void{
			//分割左右页
			var wtem:Number=target.getShowWidth();
			var htem:Number=target.getShowHeight();
			if(wtem==0) wtem=w2;
			if(htem==0) htem=this.pageH;
			var bmdata:BitmapData=new BitmapData(wtem,htem);
			bmdata.draw(target);
			w2=Math.floor(bmdata.width/2);
			currentBitmapLeft=new BitmapData(w2,bmdata.height);
			currentBitmapLeft.copyPixels(bmdata,new Rectangle(0,0,w2,bmdata.height),new Point(0,0));
			currentBitmapRight=new BitmapData(bmdata.width/2,bmdata.height);
			w2_1=bmdata.width-w2;
			currentBitmapRight.copyPixels(bmdata,new Rectangle(w2_1,0,w2_1,bmdata.height),new Point(0,0));
			//
			setNextPage();
			updatePageFlip();
			//trace("updateCurrentPageBitmapData");
			//this.addChild(new Bitmap(bmdata));
		}
		private function updatePageFlip():void{
			//trace(mousePoint_left+"  "+pleft_pt);
			PageFlip.drawBitmapSheet(
				PageFlip.computeFlip(mousePoint_left.clone(),pleft_pt,w2,pageH,true,1),
				layout_2_left,
				currentBitmapLeft,
				currentBitmapLeft_out);
			PageFlip.drawBitmapSheet(
				PageFlip.computeFlip(mousePoint_right.clone(),pright_pt,w2,pageH,true,1),
				layout_2_right,
				currentBitmapRight,
				currentBitmapRight_out);
		}
		private function setNextPage():void{
			if(nextPage!=null){
				nextPage.killMe();
				nextPage=null;
			}
			var tem123:Number=page+1;
			if(tem123>this.pageCount-1){
				nextPage=Base.getBase(Painter.getRect(this.pageW,this.pageH,0xffffff,0));
				nextPagePath="";
				
			}else{
				nextPagePath=this.arrImgPath[tem123];
				nextPage=new CLoadImg(nextPagePath);
				//
				var bmdata:BitmapData=new BitmapData(this.w2,this.pageH);
				bmdata.draw(nextPage);
				this.currentBitmapRight_out=bmdata;
			}
			nextPage.show(layout_1);
		}
		private function setPrePage():void{
			if(nextPage!=null){
				nextPage.killMe();
				nextPage=null;
			}
			var tem123:Number=page-1;
			if(tem123<0){
				nextPage=Base.getBase(Painter.getRect(this.pageW,this.pageH,0xffffff,0));
				nextPagePath="";
				
			}else{
				nextPagePath=this.arrImgPath[tem123];
				nextPage=new CLoadImg(nextPagePath);
				//
				var bmdata:BitmapData=new BitmapData(this.pageW,this.pageH);
				bmdata.draw(nextPage);
				var imgTem:BitmapData=new BitmapData(this.w2_1,this.pageH);
				imgTem.copyPixels(bmdata,new Rectangle(w2,0,w2_1,this.pageH),new Point());
				this.currentBitmapLeft_out=imgTem;
			}
			nextPage.show(layout_1);
		}
		//左右翻页效果容器深度切换
		private function swapDepath_layout_2_left_right(isLeft:Boolean=true):void{
			var leftTem:Number=this.getChildIndex(this.layout_2_left);
			var rightTem:Number=this.getChildIndex(this.layout_2_right);
			if(isLeft){
				if(leftTem<rightTem) this.swapChildren(this.layout_2_left,this.layout_2_right);
			}else{
				if(leftTem>rightTem) this.swapChildren(this.layout_2_left,this.layout_2_right);
			}
		}
		//显示或隐藏所有触点		
		private function showHideAllTouchPoint(isShow:Boolean=true):void{
			touchPoint1.visible=isShow;
			touchPoint2.visible=isShow;
			touchPoint3.visible=isShow;
			touchPoint4.visible=isShow;
		}
		private function showHidePageFlip(isShow:Boolean=true):void{
			if(isShow){
				updateCurrentPageBitmapData(currentPage);
			}
			layout_2_left.visible=isShow;
			layout_2_right.visible=isShow;
		}
		//点击触点
		private function down_touchPoint(e:MouseEvent):void{
			switch(e.target){
				case touchPoint1:
					touchPoint_type=1;
					pleft_pt=new Point(0,0)
					this.mousePoint_left=new Point(layout_2_left.mouseX,layout_2_left.mouseY);
					isMouseLeftRight=true;
					//
					swapDepath_layout_2_left_right();
					//
					setPrePage()
					updatePageFlip();
					break;
				case touchPoint2:
					touchPoint_type=2;
					pright_pt=new Point(1,0);
					this.mousePoint_right=new Point(layout_2_right.mouseX,layout_2_right.mouseY);
					isMouseLeftRight=false;
					//
					swapDepath_layout_2_left_right(false);
					//
					setNextPage();
					updatePageFlip();
					break;
				case touchPoint3:
					touchPoint_type=3;
					pright_pt=new Point(1,1);
					this.mousePoint_right=new Point(layout_2_right.mouseX,layout_2_right.mouseY);
					isMouseLeftRight=false;
					//
					swapDepath_layout_2_left_right(false);
					//
					setNextPage();
					updatePageFlip();
					break;
				case touchPoint4:
					touchPoint_type=4;
					pleft_pt=new Point(0,1)
					this.mousePoint_left=new Point(layout_2_left.mouseX,layout_2_left.mouseY);
					isMouseLeftRight=true;
					//
					swapDepath_layout_2_left_right();
					//
					setPrePage()
					updatePageFlip();
					break;
			}
			//trace(isMouseLeftRight)
			if(this.stage!=null){
				isMouseUp=false;
				this.stage.addEventListener(MouseEvent.MOUSE_UP,upTouchPoint);
			}
			showHideAllTouchPoint(false)
		}
		private function upTouchPoint(e:MouseEvent):void{
			isMouseUp=true;			
			showHideAllTouchPoint();
			//松开的坐标，如果超过一半，说明是翻页
			if(touchPoint_type==1||touchPoint_type==4){
				//向右
				if(this.mouseX>w2){
					//是
					this._NextPage()
				}else{
					//不是
					if(touchPoint_type==1){
						this.mousePoint_left=new Point(0,0);
					}else{
						this.mousePoint_left=new Point(0,this.pageH);
					}
				}
			}else{
				//向左
				if(this.mouseX<w2){
					//是
					this._PrePage();
				}else{
					//不是
					if(touchPoint_type==2){
						this.mousePoint_right=new Point(this.w2_1,0);
					}else{
						this.mousePoint_right=new Point(this.w2_1,this.pageH);
					}
				}
			}
			//
			updatePageFlip();
			touchPoint_type=0;
			this.stage.removeEventListener(MouseEvent.MOUSE_UP,upTouchPoint);
		}
		private function enter_frame(e:Event):void{
			
			if(!isMouseUp){
				if(isMouseLeftRight){
					this.mousePoint_left=new Point(layout_2_left.mouseX,layout_2_left.mouseY);
				}else{
					this.mousePoint_right=new Point(layout_2_right.mouseX,layout_2_right.mouseY);
				}
				//
				updatePageFlip();
			}
		}
	}
}